<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="老师ID" prop="teacherId">
      <el-input v-model="dataForm.teacherId" placeholder="老师ID"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="createTime">
      <el-input type="datetime" v-model="dataForm.createTime" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="参加人数" prop="jointimes">
      <el-input v-model="dataForm.jointimes" placeholder="参加人数"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input type="datetime" v-model="dataForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input type="datetime" v-model="dataForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="课程描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="课程描述"></el-input>
    </el-form-item>
    <el-form-item label="课程目标" prop="goal">
      <el-input v-model="dataForm.goal" placeholder="课程目标"></el-input>
    </el-form-item>
    <el-form-item label="课程大纲" prop="outline">
      <el-input v-model="dataForm.outline" placeholder="课程大纲"></el-input>
    </el-form-item>
    <el-form-item label="课程价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="课程价格"></el-input>
    </el-form-item>

      <el-upload
        class="avatar-uploader"
        action="http://127.0.0.1:8081/backstage/course/uploadImage"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

    <el-form-item label="类型难度" prop="courseLevel">
      <el-input v-model="dataForm.courseLevel" placeholder="类型难度"></el-input>
    </el-form-item>
    <el-form-item label="老师名字" prop="teachername">
      <el-input v-model="dataForm.teachername" placeholder="老师名字"></el-input>
    </el-form-item>
    <el-form-item label="章节ID" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="章节ID"></el-input>
    </el-form-item>
    <el-form-item label="分类ID" prop="areaId">
      <el-input v-model="dataForm.areaId" placeholder="分类ID"></el-input>
    </el-form-item>
    <el-form-item label="总时长" prop="totalDuration">
      <el-input v-model="dataForm.totalDuration" placeholder="总时长"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="stock">
      <el-input v-model="dataForm.stock" placeholder="库存"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        imageUrl: '',
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          teacherId: '',
          createTime: '',
          jointimes: '',
          startTime: '',
          endTime: '',
          description: '',
          goal: '',
          outline: '',
          price: '',
          coursePicture: '',
          courseLevel: '',
          teachername: '',
          cid: '',
          areaId: '',
          totalDuration: '',
          stock: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          teacherId: [
            { required: true, message: '老师ID不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          jointimes: [
            { required: true, message: '参加人数不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '课程描述不能为空', trigger: 'blur' }
          ],
          goal: [
            { required: true, message: '课程目标不能为空', trigger: 'blur' }
          ],
          outline: [
            { required: true, message: '课程大纲不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '课程价格不能为空', trigger: 'blur' }
          ],
          coursePicture: [
            { required: true, message: '课程封面图不能为空', trigger: 'blur' }
          ],
          courseLevel: [
            { required: true, message: '类型难度不能为空', trigger: 'blur' }
          ],
          teachername: [
            { required: true, message: '老师名字不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '章节ID不能为空', trigger: 'blur' }
          ],
          areaId: [
            { required: true, message: '分类ID不能为空', trigger: 'blur' }
          ],
          totalDuration: [
            { required: true, message: '总时长不能为空', trigger: 'blur' }
          ],
          stock: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/coursespike/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.coursespike.title
                this.dataForm.teacherId = data.coursespike.teacherId
                this.dataForm.createTime = data.coursespike.createTime
                this.dataForm.jointimes = data.coursespike.jointimes
                this.dataForm.startTime = data.coursespike.startTime
                this.dataForm.endTime = data.coursespike.endTime
                this.dataForm.description = data.coursespike.description
                this.dataForm.goal = data.coursespike.goal
                this.dataForm.outline = data.coursespike.outline
                this.dataForm.price = data.coursespike.price
                this.dataForm.coursePicture = data.coursespike.coursePicture
                this.dataForm.courseLevel = data.coursespike.courseLevel
                this.dataForm.teachername = data.coursespike.teachername
                this.dataForm.cid = data.coursespike.cid
                this.dataForm.areaId = data.coursespike.areaId
                this.dataForm.totalDuration = data.coursespike.totalDuration
                this.dataForm.stock = data.coursespike.stock
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/coursespike/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'teacherId': this.dataForm.teacherId,
                'createTime': this.dataForm.createTime,
                'jointimes': this.dataForm.jointimes,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'description': this.dataForm.description,
                'goal': this.dataForm.goal,
                'outline': this.dataForm.outline,
                'price': this.dataForm.price,
                'coursePicture': this.dataForm.coursePicture,
                'courseLevel': this.dataForm.courseLevel,
                'teachername': this.dataForm.teachername,
                'cid': this.dataForm.cid,
                'areaId': this.dataForm.areaId,
                'totalDuration': this.dataForm.totalDuration,
                'stock': this.dataForm.stock
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleAvatarSuccess (res, file) {
        console.log(res)
        this.dataForm.coursePicture = res.data
        this.imageUrl = res.data
        // this.imageUrl = URL.createObjectURL(file.raw)
      },
      beforeAvatarUpload (file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2

        // console.log(file)
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      }
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-left: 300px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 70px;
    height: 70px;
    line-height: 70px;
    text-align: center;
  }
  .avatar {
    width: 70px;
    height: 70px;
    display: block;
  }
</style>
